package com.ukefu.webim.util.impl;

import java.io.IOException;

import org.elasticsearch.common.lang3.ArrayUtils;
import org.elasticsearch.common.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.lionsoul.jcseg.tokenizer.core.JcsegException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import com.ukefu.core.UKDataContext;
import com.ukefu.util.ai.AiUtils;
import com.ukefu.util.ai.DicSegment;
import com.ukefu.webim.service.repository.ChatMessageRepository;
import com.ukefu.webim.service.repository.TopicRepository;
import com.ukefu.webim.util.event.AiMessageAskProcesser;
import com.ukefu.webim.util.server.message.ChatMessage;
import com.ukefu.webim.web.model.AiConfig;
import com.ukefu.webim.web.model.Topic;

@Component
public class AiMessageAskProcesserImpl implements AiMessageAskProcesser{
	
	@Autowired
	private ChatMessageRepository chatMessageRes ;
	
	@Autowired
	private TopicRepository topicRes ;
	
	@Override
	public ChatMessage process(ChatMessage message) {
		ChatMessage retChatMessage = null ;
		if(!StringUtils.isBlank(message.getMessage())){
			AiConfig aiConfig = AiUtils.initAiConfig(UKDataContext.SYSTEM_ORGI) ;
			BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
			try {
				String[] keywords = null ;
				if(aiConfig.isEnablekeyword()){
					keywords = DicSegment.keyword(message.getMessage() , aiConfig.getKeywordnum());
				}
				String[] tokens = DicSegment.segment(message.getMessage());
				
				if(keywords!=null && keywords.length > 0){
					for(String keyword: keywords){
						boolQueryBuilder.must(new QueryStringQueryBuilder(keyword).field("title").defaultOperator(Operator.AND)) ;
					}
				}
				BoolQueryBuilder subQueryBuilder = QueryBuilders.boolQuery();
				if(tokens!=null && tokens.length > 0){
					for(String token : tokens){
						if(!ArrayUtils.contains(keywords, token)){
							if(token.length() == 1 ){	//单字
								subQueryBuilder.must(new QueryStringQueryBuilder(token).field("title").defaultOperator(Operator.AND)) ;
							}else{
								subQueryBuilder.should(new QueryStringQueryBuilder(token).field("title").defaultOperator(Operator.AND)) ;
							}
						}
					}
				}
				boolQueryBuilder.must(subQueryBuilder) ;
				
				/**
				 * 在有效期内
				 */
				
				Page<Topic> result = topicRes.getTopicByCon(boolQueryBuilder, 0, 5) ;
				if(result.getContent()!=null && result.getContent().size() > 0){
					Topic topic = result.getContent().get(0) ;
					retChatMessage = new ChatMessage();
					retChatMessage.setMessage(topic.getContent());
					retChatMessage.setAppid(message.getAppid());
					retChatMessage.setUserid(message.getUserid());
					retChatMessage.setUsername("小E");
					retChatMessage.setChannel(message.getChannel());
					retChatMessage.setType(message.getType());
					retChatMessage.setContextid(message.getContextid());
					retChatMessage.setOrgi(message.getOrgi());
					
					/**
					 * 命中结果，记录日志，记录DEBUG信息，下个版本增加功能
					 */
				}
			} catch (IOException | JcsegException e) {
				e.printStackTrace();
			}
		}
		return retChatMessage;
	}

}
